package io.datajek.spring.basics.movierecommendersystem.partone.nine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;

import java.util.Arrays;

@SpringBootApplication
//@ComponentScan(basePackages="io.datajek.spring.basics.movierecommendersystem.partone.ten")
//@ComponentScan(includeFilters = @ComponentScan.Filter (type= FilterType.REGEX, pattern="io.datajek.spring.basics.movierecommendersystem.partone.nine*"))
public class MovieRecommenderSystemApplication {

    public static void main(String[] args) {


        //ApplicationContext负责管理Bean和依赖关系


        //ContentBasedFilter contentBasedFilter = new ContentBasedFilter();

        //SpringApplication.run(MovieRecommenderSystemApplication.class, args);

        //使用ApplicationContext来查找正在使用的过滤器
        //ApplicationContext负责管理Bean和依赖关系
        ApplicationContext applicationContext = SpringApplication.run(MovieRecommenderSystemApplication.class,args);

        //从Application中获得并打印Singleton Bean
        ContentBasedFilter filter = applicationContext.getBean(ContentBasedFilter.class);
        System.out.println("\nContentBasedFilter bean with singleton scope");
        System.out.println(filter);

        //从Singleton Bean中获得并打印Prototype Bean两次
        Movie movie1 = filter.getMovie();
        Movie movie2 = filter.getMovie();
        Movie movie3 = filter.getMovie();

        System.out.println("\nMovie bean with prototype scope");
        System.out.println(movie1);
        System.out.println(movie2);
        System.out.println(movie3);

        //打印每种Bean的实例数
        System.out.println("\nContentBasedFilter instances created: "+ ContentBasedFilter.getInstances());
        System.out.println("Movie instances created: "+ Movie.getInstances());

        System.out.println("ContentBasedFilter bean found = " + applicationContext.containsBean("contentBasedFilter"));
        System.out.println("CollaborativeFilter bean found = " + applicationContext.containsBean("collaborativeFilter"));

    }

        //从Application Context中三次获得Singleton Bean
//        ContentBasedFilter contentBasedFilter1 = applicationContext.getBean(ContentBasedFilter.class);
//        ContentBasedFilter contentBasedFilter2 = applicationContext.getBean(ContentBasedFilter.class);
//        ContentBasedFilter contentBasedFilter3 = applicationContext.getBean(ContentBasedFilter.class);

        //从Application Context中三次获得Prototype Bean
//        ContentBasedFilter cf1 = applicationContext.getBean(CollaborativeFilter.class);
//        ContentBasedFilter cf2 = applicationContext.getBean(CollaborativeFilter.class);
//        ContentBasedFilter cf3 = applicationContext.getBean(CollaborativeFilter.class);

//        System.out.println(contentBasedFilter1);
//        System.out.println(contentBasedFilter2);
//        System.out.println(contentBasedFilter3);
//
//
//        System.out.println(cf1);
//        System.out.println(cf2);
//        System.out.println(cf3);



/*      //RecommenderImplementation使用构造函数注入依赖关系
        System.out.println("\n*************************************************");
        System.out.println("Calling getBean() on RecommenderImplementation");

        //创建 RecommenderImplementation 类的对象
        RecommenderImplementation recommender = applicationContext.getBean(RecommenderImplementation.class);


        //创建 RecommenderImplementation 类的对象
        //RecommenderImplementation recommender = new RecommenderImplementation();


        String[] result = recommender.recommendMovies("Finding Dory");

        System.out.println(Arrays.toString(result));


        //RecommenderImplementation2使用setter方法注入依赖关系
        System.out.println("\n*************************************************");
        System.out.println("Calling getBean() on RecommenderImplementation2");
        RecommenderImplementation2 recommender2 = applicationContext.getBean(RecommenderImplementation2.class);
        result = recommender2.recommendMovies("Finding Dory");
        System.out.println(Arrays.toString(result));



*/
    }

